local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local HomeGui=ClientOHOH.load.UIManager().ui_list.HomeGui
local UiScript=script.StartPackGui

local tool=require(script.Parent.tool)

local gTrainCount=tool.Count --训练X次触发新手礼包购买
local gLimitTime=tool.LimitTime --限制购买时间
local gid=tool.GoodsId

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);	
    HomeGui.insts.StarterPack.root.Visible=false
    self.DataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self.player=self.parent
    if  not self.DataBase:GetData("sate").fristPackage then
        self:Init()
    end
end;
function M.Init(self)


    self.gui=ClientOHOH.load.UIManager():CreateUI(UiScript.Name,require(UiScript))

     local data=tool.GetPackDes()

     self.gui:FullData(data.weapon,data.wins,data.diamond,data.pets,data.price)

    self.gui.BuyEvent:Connect(function()
        self.player.RobuxBuyEvent:Action(gid)
    end)

    self.player.CanNotRobuxBuyEvent:Connect(function(id,gtype,...)
        local isCanBuy=tool.IsCanBuy(id,gtype,self.DataBase)
        return  isCanBuy
    end)

    ClientOHOH.load.CallBackManager():AddRbxConnect(self, self.player.UserDataEvent, function(player,data)
        if data["sate"] and data["sate"].fristPackage then
            self:Hide()
            self:dtor()
        end
    end);

   -- 训练到30次的时候，触发
   local TrainEventCon=nil
   local sum=gTrainCount

   TrainEventCon= self.player.OneTrainEvent:Connect(function()
        sum=sum-1
        if sum==0 then

            if TrainEventCon then
                TrainEventCon:Disconnect()
                TrainEventCon=nil
            end
            self:FristShow()
        end

    end)


end

function M.FristShow(self)
   local con=nil
   con= self.gui.BaseHideEvent:Connect(function()
        if con then
            con:Disconnect()
            con=nil
        end
        self:LimitTime()
   end)
   ClientOHOH.load.UIManager():PushShow("StartPackGui")
end

function M.LimitTime(self)
    HomeGui.insts.StarterPack.root.Visible=true
    self.SumTime=gLimitTime -- 倒计时半小时
    local GeneralTool=_G.CommonOHOH.load.GeneralTool();
    HomeGui.insts.StarterPack.tipTime.Text=GeneralTool.secondsToHHMM(self.SumTime)
    self.LimitCon=self:AddStepUpdate(function()
        self.SumTime=self.SumTime-1
        HomeGui.insts.StarterPack.tipTime.Text=GeneralTool.secondsToHHMM(self.SumTime)
        if self.SumTime==0 then
            self:dtor()
        end
    end,1)

    ClientOHOH.load.CallBackManager():AddRbxConnect(self, HomeGui.insts.StarterPack.button.Activated, function()
         self.gui:Show()
    end);

end

function M.Hide(self)
    
    if self.LimitCon then
        self.LimitCon:Disconnect()
        self.LimitCon=nil
    end
    if self.gui then
        self.gui:Hide()
    end

    HomeGui.insts.StarterPack.root.Visible=false
end

function M.dtor(self)

	M.super.dtor(self);

end;

return M